using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySQL_SysFrameMVC.Message;
using System.Reflection;
using System.Data;
using MySql.Data.MySqlClient;
using SysFrame.Message;
using SysFrame.BP;
using TaskAPP.Message;

namespace TaskApp.BP
{
    /// <summary>
    /// taskapp_c1方法
    /// </summary>
    public class MySQLtaskapp_c1 : Itaskapp_c1
    {

        /// <summary>
        /// 得到taskapp_c1实体
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public List<Mstaskapp_c1> Gettaskapp_c1MsByQuery(MsQuery msq)
        {
            List<Mstaskapp_c1> listms = new List<Mstaskapp_c1>();
            StringBuilder sb = new StringBuilder();
            List<string> lstCmdText = new List<string>();
            sb.Append("select taskapp_c1.*");
            sb.Append(" ,a51.ASSET_A5_10 as strTASKAPP_C1_10");
            sb.Append(" ,a52.ASSET_A5_10 as strTASKAPP_C1_20 ");
            sb.Append(" from taskapp_c1 ");
            sb.Append(" LEFT JOIN ASSET_A5 a51 on a51.ASSET_A5_AUTOID=TASKAPP_C1_10 ");
            sb.Append(" LEFT JOIN ASSET_A5 a52 on a52.ASSET_A5_AUTOID=TASKAPP_C1_20 ");
            sb.Append(" where 1=1 ").Append(msq.strquery);
            if (msq.strorder != null && msq.strorder != "")
            {
                sb.Append(" order by ").Append(msq.strorder);
            }
            lstCmdText.Add(sb.ToString());
            sb.Remove(0, sb.ToString().Length);
            try
            {
                DataSet ds = DbHelperMySQL.Query(lstCmdText[0].ToString());
                if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        Mstaskapp_c1 ms = new Mstaskapp_c1();
                        int? TASKAPP_C1_AUTOID = null;
                        ms.TASKAPP_C1_AUTOID =(int)dr["TASKAPP_C1_AUTOID"];// 单据ID
                        int? TASKAPP_C1_10 = null;
                        ms.TASKAPP_C1_10 = dr["TASKAPP_C1_10"].ToString() == "" ? TASKAPP_C1_10 : (int)dr["TASKAPP_C1_10"];// 调出存放地ID
                        int? TASKAPP_C1_20 = null;
                        ms.TASKAPP_C1_20 = dr["TASKAPP_C1_20"].ToString() == "" ? TASKAPP_C1_20 : (int)dr["TASKAPP_C1_20"];// 调入存放地ID
                        ms.strTASKAPP_C1_10 = dr["strTASKAPP_C1_10"].ToString();// 调出存放地
                        ms.strTASKAPP_C1_20 = dr["strTASKAPP_C1_20"].ToString();// 调入存放地

                        ms.TASKAPP_C1_30 = dr["TASKAPP_C1_30"].ToString();// 备注

                        ms.TASKAPP_C1_40 = dr["TASKAPP_C1_40"].ToString();// 备用1

                        ms.TASKAPP_C1_50 = dr["TASKAPP_C1_50"].ToString();// 备用2

                        ms.TASKAPP_C1_60 = dr["TASKAPP_C1_60"].ToString();// 

                        ms.TASKAPP_C1_70 = dr["TASKAPP_C1_70"].ToString();// 

                        ms.TASKAPP_C1_80 = dr["TASKAPP_C1_80"].ToString();// 

                        ms.TASKAPP_C1_90 = dr["TASKAPP_C1_90"].ToString();// 

                        ms.TASKAPP_C1_100 = dr["TASKAPP_C1_100"].ToString();// 
                        

                        listms.Add(ms);
                    }
                }
            }
            catch { }
            return listms;
        }

        /// <summary>
        ///  保存taskapp_c1信息
        /// </summary>
        /// <param name="ms">实体信息 通过TASKAPP_C1_AUTOID进行判断有TASKAPP_C1_AUTOID就是update</param>
        /// <returns>消息</returns>
        public MsReturned Savetaskapp_c1(Mstaskapp_c1 ms)
        {
            MsReturned msr = new MsReturned();
            if (ms.TASKAPP_C1_AUTOID != null && ms.TASKAPP_C1_AUTOID.ToString() != "" && ms.TASKAPP_C1_AUTOID.ToString() != "0")
            {
                msr = Uptaskapp_c1(ms);
            }
            else
            {
                msr = Addtaskapp_c1(ms);
            }
            return msr;
        }

        /// <summary>
        ///  新增taskapp_c1信息
        /// </summary>
        /// <param name="ms">实体信息 通过TASKAPP_C1_AUTOID进行判断有TASKAPP_C1_AUTOID就是update</param>
        /// <returns>消息</returns>
        public MsReturned Addtaskapp_c1(Mstaskapp_c1 ms)
        {
            List<string> lstResult = new List<string>();
            StringBuilder sb = new StringBuilder();
            List<string> lstCmdText = new List<string>();
            MsReturned msr = new MsReturned();
            int newRateId = Convert.ToInt32(ms.typecode1);//typecode1为AutoID
            sb.Append("insert into taskapp_c1 (");
            sb.Append("TASKAPP_C1_AUTOID,TASKAPP_C1_10,TASKAPP_C1_20,TASKAPP_C1_30,TASKAPP_C1_40,");
            sb.Append("TASKAPP_C1_50,TASKAPP_C1_60,TASKAPP_C1_70,TASKAPP_C1_80,TASKAPP_C1_90,");
            sb.Append("TASKAPP_C1_100");
            sb.Append(")");
            sb.Append("values ( ");
            sb.Append(newRateId.ToString()).Append(",");
            sb.Append("'");
            sb.Append(ms.TASKAPP_C1_10).Append("','");//TASKAPP_C1_10 调出存放地ID
            sb.Append(ms.TASKAPP_C1_20).Append("','");//TASKAPP_C1_20 调入存放地ID
            sb.Append(ms.TASKAPP_C1_30).Append("','");//TASKAPP_C1_30 备注
            sb.Append(ms.TASKAPP_C1_40).Append("','");//TASKAPP_C1_40 备用1
            sb.Append(ms.TASKAPP_C1_50).Append("','");//TASKAPP_C1_50 备用2
            sb.Append(ms.TASKAPP_C1_60).Append("','");//TASKAPP_C1_60 
            sb.Append(ms.TASKAPP_C1_70).Append("','");//TASKAPP_C1_70 
            sb.Append(ms.TASKAPP_C1_80).Append("','");//TASKAPP_C1_80 
            sb.Append(ms.TASKAPP_C1_90).Append("','");//TASKAPP_C1_90 
            sb.Append(ms.TASKAPP_C1_100).Append("')");//TASKAPP_C1_100 
            MySqlParameter[] parameters = null;
            lstCmdText.Add(sb.ToString());
            sb.Remove(0, sb.ToString().Length);
            try
            {
                int iexe = DbHelperMySQL.ExecuteSql(lstCmdText[0].ToString(), parameters);
                if (iexe > 0)
                {
                    msr.strMsId = newRateId.ToString();
                    msr.strMS = "保存成功!";
                }
                else
                {
                    msr.strMsId = "-1";
                    msr.strMS = "操作失败:服务器忙,请稍后再试!";
                }
            }
            catch(Exception ex)
            {
                msr.strMsId = "-1";
                msr.strMS = "操作失败:服务器忙,请稍后再试!";
            }
            lstCmdText = null;
            sb = null;
            return msr;
        }

        /// <summary>
        ///  更新taskapp_c1信息
        /// </summary>
        /// <param name="ms">实体信息 通过TASKAPP_C1_AUTOID进行判断有TASKAPP_C1_AUTOID就是update</param>
        /// <returns>消息</returns>
        public MsReturned Uptaskapp_c1(Mstaskapp_c1 ms)
        {
            List<string> lstResult = new List<string>();
            StringBuilder sb = new StringBuilder();
            List<string> lstCmdText = new List<string>();
            MsReturned msr = new MsReturned();
            sb.Append("update taskapp_c1 set ");
            sb.Append(" TASKAPP_C1_AUTOID='").Append(ms.TASKAPP_C1_AUTOID).Append("' ");// TASKAPP_C1_AUTOID 单据ID
            if (ms.TASKAPP_C1_10 != null && ms.TASKAPP_C1_10.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_10='").Append(ms.TASKAPP_C1_10).Append("' ");// TASKAPP_C1_10 调出存放地ID
            }
            if (ms.TASKAPP_C1_20 != null && ms.TASKAPP_C1_20.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_20='").Append(ms.TASKAPP_C1_20).Append("' ");// TASKAPP_C1_20 调入存放地ID
            }
            if (ms.TASKAPP_C1_30 != null && ms.TASKAPP_C1_30.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_30='").Append(ms.TASKAPP_C1_30).Append("' ");// TASKAPP_C1_30 备注
            }
            if (ms.TASKAPP_C1_40 != null && ms.TASKAPP_C1_40.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_40='").Append(ms.TASKAPP_C1_40).Append("' ");// TASKAPP_C1_40 备用1
            }
            if (ms.TASKAPP_C1_50 != null && ms.TASKAPP_C1_50.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_50='").Append(ms.TASKAPP_C1_50).Append("' ");// TASKAPP_C1_50 备用2
            }
            if (ms.TASKAPP_C1_60 != null && ms.TASKAPP_C1_60.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_60='").Append(ms.TASKAPP_C1_60).Append("' ");// TASKAPP_C1_60 
            }
            if (ms.TASKAPP_C1_70 != null && ms.TASKAPP_C1_70.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_70='").Append(ms.TASKAPP_C1_70).Append("' ");// TASKAPP_C1_70 
            }
            if (ms.TASKAPP_C1_80 != null && ms.TASKAPP_C1_80.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_80='").Append(ms.TASKAPP_C1_80).Append("' ");// TASKAPP_C1_80 
            }
            if (ms.TASKAPP_C1_90 != null && ms.TASKAPP_C1_90.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_90='").Append(ms.TASKAPP_C1_90).Append("' ");// TASKAPP_C1_90 
            }
            if (ms.TASKAPP_C1_100 != null && ms.TASKAPP_C1_100.ToString() != "")
            {
                sb.Append(",").Append(" TASKAPP_C1_100='").Append(ms.TASKAPP_C1_100).Append("' ");// TASKAPP_C1_100 
            }
            sb.Append(" where TASKAPP_C1_AUTOID='").Append(ms.TASKAPP_C1_AUTOID).Append("'");// TASKAPP_C1_AUTOID 单据ID
            lstCmdText.Add(sb.ToString());
            sb.Remove(0, sb.ToString().Length);
            try
            {
                int iexe = DbHelperMySQL.ExecuteSql(lstCmdText[0].ToString());
                if (iexe > 0)
                {
                    msr.strMsId = ms.TASKAPP_C1_AUTOID.ToString();
                    msr.strMS = "保存成功!";
                }
                else
                {
                    msr.strMsId = "-1";
                    msr.strMS = "操作失败:服务器忙,请稍后再试!";
                }
            }
            catch
            {
                msr.strMsId = "-1";
                msr.strMS = "操作失败:服务器忙,请稍后再试!";
            }
            lstCmdText = null;
            sb = null;
            return msr;
        }

        /// <summary>
        /// 得到数据类型
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns>数据类型</returns>
        public MsReturned Gettaskapp_c1ByQuery(MsQuery msq)
        {
            DataSet ds = null;
            MsReturned msr = new MsReturned();
            StringBuilder sb = new StringBuilder();
            List<string> lstCmdText = new List<string>();
            List<string> tasksql = MySQLtaskapp_main.TaskMainSql();
            sb.Append("select taskapp_c1.* ");
            sb.Append(tasksql[0]);
            sb.Append(" ,a51.ASSET_A5_10 as strTASKAPP_C1_10");//调出存放地
            sb.Append(" ,a52.ASSET_A5_10 as strTASKAPP_C1_20 ");//调入存放地
            sb.Append(" from taskapp_c1 ");
            sb.Append(" LEFT JOIN TASKAPP_MAIN main on main.TASKAPP_MAIN_AUTOID=TASKAPP_C1_AUTOID ");
            sb.Append(tasksql[1]);
            sb.Append(" LEFT JOIN ASSET_A5 a51 on a51.ASSET_A5_AUTOID=TASKAPP_C1_10 ");//调出存放地
            sb.Append(" LEFT JOIN ASSET_A5 a52 on a52.ASSET_A5_AUTOID=TASKAPP_C1_20 ");//调入存放地
            sb.Append(" where 1=1 ").Append(msq.strquery);
            if (msq.strorder != null && msq.strorder != "")
            {
                sb.Append(" order by ").Append(msq.strorder);
            }
            lstCmdText.Add(sb.ToString());
            sb.Remove(0, sb.ToString().Length);
            try
            {
                ds = DbHelperMySQL.Query(lstCmdText[0].ToString());
            }
            catch { }
            msr.data = ds;
            return msr;
        }

        /// <summary>
        /// 删除taskapp_c1
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deletetaskapp_c1ByQuery(MsQuery msq)
        {
            StringBuilder sb = new StringBuilder();
            MsReturned msr = new MsReturned();
            sb.Append(" delete from taskapp_c1 where TASKAPP_C1_AUTOID > 0 ").Append(msq.strquery);
            try
            {
                int iexe = DbHelperMySQL.ExecuteSql(sb.ToString());
                if (iexe > 0)
                {
                    msr.strMsId = "1";
                    msr.strMS = "保存成功!";
                }
                else
                {
                    msr.strMsId = "-1";
                    msr.strMS = "操作失败:服务器忙,请稍后再试!";
                }
            }
            catch
            {
                msr.strMsId = "-1";
                msr.strMS = "操作失败:服务器忙,请稍后再试!";
            }
            sb = null;
            return msr;
        }

    }
}
